package com.zzsy.carosshopprojects.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.king.base.adapter.ViewHolderRecyclerAdapter;
import com.king.base.adapter.holder.ViewHolder;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zzsy.carosshopprojects.R;
import com.zzsy.carosshopprojects.activitys.home.mainpage.menu.changeshifts.ChangeShiftsAc;
import com.zzsy.carosshopprojects.bean.MenuBean;

import java.util.List;

/**
 * 首页抽屉适配器
 */
public class MenuAdapter extends ViewHolderRecyclerAdapter<MenuBean> {
    public static final int TYPE_ITEM_TITLE = 0;//标题
    public static final int TYPE_ITEM_BASE = 1;//基本信息
    public static final int TYPE_ITEM_INTERVAL = 2;//间隔
    public static final int TYPE_ITEM_HISTORY = 3;//交接班历史记录
    public static final int TYPE_ITEM_WHETHER = 4;//是否

    private OnSwithChageListener onSwithChageListener;

    public MenuAdapter(Context context, List<MenuBean> listData, int layoutId) {
        super(context, listData, layoutId);
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, ViewGroup parent, int viewType) {
        View view = null;
        switch (viewType) {
            case TYPE_ITEM_TITLE:
                view = layoutInflater.inflate(R.layout.adapter_item_title, null);
                break;
            case TYPE_ITEM_BASE:
                view = layoutInflater.inflate(R.layout.adapter_item_base, null);
                break;
            case TYPE_ITEM_INTERVAL:
                view = layoutInflater.inflate(R.layout.adapter_item_interval, null);
                break;
            case TYPE_ITEM_HISTORY:
                view = layoutInflater.inflate(R.layout.adapter_item_history, null);
                break;
            case TYPE_ITEM_WHETHER:
                view = layoutInflater.inflate(R.layout.adapter_item_whether, null);
                break;

        }
        ScreenAdapterTools.getInstance().loadView(view);
        return view;
    }

    @Override
    public void bindViewDatas(ViewHolder helper, MenuBean bean, int positions) {
        switch (bean.getViewType()) {
            case TYPE_ITEM_TITLE:
                helper.setText(R.id.tv_title, bean.getTitle());
                helper.setText(R.id.tv_subTitle, bean.getSubtitle());
                helper.setText(R.id.tv_name, bean.getContent());
                break;
            case TYPE_ITEM_HISTORY:
                helper.setText(R.id.tv_title, bean.getTitle());
                helper.setOnClickListener(R.id.cl_history, v -> {
                    ChangeShiftsAc.start(getContext());
                });
                break;
            case TYPE_ITEM_WHETHER:
                helper.setText(R.id.tv_title, bean.getTitle());
                Button checkBox = helper.getView(R.id.cb_fa_box);
                if (bean.getState().equals("1"))
                    checkBox.setBackgroundResource(R.mipmap.kauguan_kai);
                else
                    checkBox.setBackgroundResource(R.mipmap.kaiguan_guan);
                checkBox.setOnClickListener(view -> {
                    onSwithChageListener.onSwitch(bean.getState().equals("1") ? false : true, bean.getSwitchType());
                });

                break;
            case TYPE_ITEM_BASE:
                helper.setText(R.id.tv_title, bean.getTitle());
                helper.setText(R.id.tv_start_name, bean.getContent());
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        if (getListData().get(position).getViewType() == TYPE_ITEM_TITLE)
            return TYPE_ITEM_TITLE;
        else if (getListData().get(position).getViewType() == TYPE_ITEM_INTERVAL)
            return TYPE_ITEM_INTERVAL;
        else if (getListData().get(position).getViewType() == TYPE_ITEM_HISTORY)
            return TYPE_ITEM_HISTORY;
        else if (getListData().get(position).getViewType() == TYPE_ITEM_WHETHER)
            return TYPE_ITEM_WHETHER;
        else
            return TYPE_ITEM_BASE;
    }

    public interface OnSwithChageListener {
        void onSwitch(boolean isSwitch, String type);
    }

    public void setListener(OnSwithChageListener onSwithChageListener) {
        this.onSwithChageListener = onSwithChageListener;
    }

    /**
     * 如果开启发票开关失败 则还原之前的状态
     *
     * @param state
     */
    public void setCheckState(String state, String type) {
        for (int i = 0; i < getListData().size(); i++) {
            if (getListData().get(i).getViewType() == TYPE_ITEM_WHETHER && getListData().get(i).getSwitchType().equals(type)) {
                getListData().get(i).setState(state);
                notifyItemChanged(i);
            }
        }

    }


}
